/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.dorian.atomicuml.dao;

import com.dorian.atomicuml.dao.interfaces.DiagrammsProjectsManager;
import com.dorian.atomicuml.orm.DiagrammsProjects;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

/**
 *
 * @author cedric.creusot
 */
public class DiagrammsProjectsManagerImpl implements DiagrammsProjectsManager {
    
    private SessionFactory sessionFactory;
   
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    @Override
    public boolean addDiagramms(int projectId, int diagrammsId) {
        Session session = this.sessionFactory.getCurrentSession();
        
        try
        {
            DiagrammsProjects dp = new DiagrammsProjects(0, diagrammsId, projectId);
            session.save(dp);
            return true;
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean deleteDiagramms(int projectId, int diagrammsId) {
        Session session = this.sessionFactory.getCurrentSession();
        
        try
        {
            Query query = session.getNamedQuery("DiagrammsProjecs.findByDiagrammsIdAndProjectsId")
                                 .setParameter("projectsId", projectId)
                                 .setParameter("diagrammsId", diagrammsId);
            session.delete(query.uniqueResult());
            return true;
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public List<DiagrammsProjects> getDiagrammsProject(int projectId) {
        Session session = this.sessionFactory.getCurrentSession();
        List< DiagrammsProjects > dp = null;
        
        try
        {
            Query query = session.createSQLQuery("select * from DIAGRAMMS_PROJECTS where projects_id = :projects_id")
                                 .addEntity(DiagrammsProjects.class)
                                 .setParameter("projects_id", projectId);
            dp = query.list();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return dp;
    }

    @Override
    public boolean addDiagramms(String projectTitle, int diagrammsId) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public boolean deleteDiagramms(String projectTitle, int diagrammsId) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public List<DiagrammsProjects> getDiagrammsProject(String projectTitle) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public boolean isDiagrammsPartOfTheProject(int projectId, int diagId) {
        Session session = this.sessionFactory.getCurrentSession();
        try
        {
            Query query = session.getNamedQuery("DiagrammsProjecs.findByDiagrammsIdAndProjectsId")
                                 .setParameter("projectsId", projectId)
                                 .setParameter("diagrammsId", diagId);
            return (query.uniqueResult() != null);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return false;
    }

}
